<?php
class product_model extends jqxGrid_CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    
    function get($ProductID)
    {
        $query = $this->db->query("
            SELECT * 
            FROM `dtb_product` 
            WHERE `Delete` is null
                AND `ProductID`='$ProductID'
        ");
        return $query->result();
    }
    function getProductToken($q)
    {
        $vowels = array( "(", ")", "'","*","&","-"," ","_","!"," ","<",">","\\");
        $q = str_replace("đ","d", $q);
        $q = str_replace($vowels,"", $q);
        $query = $this->db->query("
            SELECT ProductID,ProductName
            FROM `dtb_product` 
            WHERE  `ProductName` like '%$q%'
            ORDER BY `ProductName` ASC
            LIMIT 15
        ");
        return $query->result();
    }
    function getProductInList($list)
    {
        $query = $this->db->query("
            SELECT *
            FROM `dtb_product` 
            WHERE `Delete` is null
                AND `ProductID` in ( $list )
            ORDER BY `ProductName` ASC
        ");
        return $query->result();
    }
    function delete($ProductID){
        $query = "
            UPDATE `dtb_product` 
            SET `Delete` = now()
            WHERE `ProductID`='$ProductID'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
        
    }
    
    function restore($ProductID){
        $query = "
            UPDATE `dtb_product` 
            SET     `Delete`    =   null
            WHERE   `ProductID`        =   '$ProductID'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    
    function changeStatus($id,$status){
        $query = "
            UPDATE `dtb_product` 
            SET `Status`    =   '$status',
                `Update`    =   now()
            WHERE `id`='$id'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    
    function insert($params){
        $this->db->set('Insert', 'NOW()', FALSE);
        $this->db->insert('dtb_product', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($ProductID,$params){
        $this->db->where(array('ProductID'=> $ProductID,'Delete'=> null));
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->update('dtb_product', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function gettotal_rows(){
        $configs["strQuery"]="
            SELECT SQL_CALC_FOUND_ROWS `dviewproduct`.*
            FROM `dviewproduct`
            ";
        $configs["strWhere"]="
            WHERE true
            ";
        if($_SESSION["ADP-PRODUCT-DEL"]==false) $configs["strWhere"].=" AND `Delete` is null ";
        $configs["strOrderBy"]=" ORDER BY `dviewproduct`.`Insert` DESC";
        $configs["fields"]=array(
            "Product"=>"`ProductID`"
            );
        $configs["usinglimit"]=true;
        $this->init($configs);
        return $this->jqxBinding();

    }
    
    
}
?>
